﻿using Bussiness.DEV;
using Common;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Bussiness
{
    public class XIMENZI : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        private Socket SocketS7;
        private Result<List<Cfg_Field>> CurReadResult=new Result<List<Cfg_Field>>();

        public Result Connect()
        {
            Result result = new Result();
            IPAddress address;
            SocketS7 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress.TryParse(Device.IP, out address);

            try
            {
                SocketS7.Connect(address, Device.Port.Value);

                ByteClass class_Connect = new ByteClass();
                int index = 0;
                byte[] buffer = new byte[] {
                3, 0, 0, 0x16, 0x11, 0xe0, 0, 0, 0, 0x48, 0, 0xc1, 2, 4, 0, 0xc2,
                2, 13, 4, 0xc0, 1, 10
                };

                for (index = 0; index < buffer[3]; index++)
                {
                    class_Connect.byte_Temp[index] = buffer[index];
                }

                //一次握手
                SocketS7.Send(class_Connect.byte_Temp, 0x16, SocketFlags.None);
                Thread.Sleep(100);
                byte[] buffer2 = new byte[] {
                3, 0, 0, 0x19, 2, 240, 0x80, 50, 1, 0, 0, 0, 1, 0, 8, 0,
                0, 240, 0, 0, 100, 0, 100, 3, 0xc0
                };

                for (index = 0; index < buffer2[3]; index++)
                {
                    class_Connect.byte_Temp[index] = buffer2[index];
                }

                //二次握手
                SocketS7.Send(class_Connect.byte_Temp, 0x19, SocketFlags.None);
                Thread.Sleep(100);
                this.SendHeart(0);
                byte[] buffer3 = new byte[] {
                3, 0, 0, 0x1d, 2, 240, 0x80, 50, 1, 0, 0, 0, 1, 0, 12, 0,
                0, 4, 1, 0x12, 8, 130, 1, 0, 20, 0, 1, 0x3b, 1
                };

                for (index = 0; index < buffer3[3]; index++)
                {
                    class_Connect.byte_Temp[index] = buffer3[index];
                }

                SocketS7.Send(class_Connect.byte_Temp, 0x1d, SocketFlags.None);
                result.Success = true;
                result.Msg = "连接成功";

                new Thread(new ThreadStart(ReviceData))
                { IsBackground = true }.Start();
            }
            catch (Exception ex)
            {
                result.Msg = ex.StackTrace;
            }

            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            bool suc=ReadData(_systemInCMD);
            //只要有一个返回true，说明通讯正常
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_workXYZCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_alarmCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_statusCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_programNameCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_toolNumberCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_spInCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_svInCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_workLeftCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_readPoweCMD);
            if (suc)
            {
                result.Success = true;
            }

            suc = ReadData(_oPCMD);
            if (suc)
            {
                result.Success = true;
            }

            foreach (var poi in CurReadResult.Data)
            {
                DevicePOI devicePOI = DevicePOIList.FirstOrDefault(p=>p.DevPOIName.Equals(poi.name));
                if (devicePOI != null)
                {
                    poi.name = devicePOI.DevPOIAlias;
                    result.Data.Add(poi);
                }
            }

            return result;
        }

        /// <summary>
        /// 发送心跳
        /// </summary>
        /// <param name="length"></param>
        private void SendHeart(int intP0)
        {
            Heart = new byte[150];
            Heart[0] = 3;
            Heart[1] = 0;
            Heart[2] = 0;
            Heart[3] = 7;
            Heart[4] = 2;
            Heart[5] = 240;
            Heart[6] = 0;
            SocketS7.Send(Heart, 7, SocketFlags.None);
        }

        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        private bool ReadData(byte[] buffer)
        {
            try
            {
                CurReadResult.Success = false;
                SocketS7.Send(buffer, Convert.ToInt32(buffer[3]), SocketFlags.None);
                int waitTime = 0;
                while (waitTime < 1000)
                {
                    if (CurReadResult.Success)
                    {
                        return true;
                    }

                    Thread.Sleep(10);
                    waitTime += 10;
                }

                CurReadResult.Msg = "未接收到数据";
                return false;
            }
            catch(Exception ex)
            {
                CurReadResult.Msg = ex.ToString();
                return false;
            }
        }

        /// <summary>
        /// 接收数据
        /// </summary>
        private void ReviceData()
        {
            while (true)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int dataLength = SocketS7.Receive(buffer, SocketFlags.None);
                    CurReadResult = DeCode(buffer, dataLength);
                }
                catch (Exception ex)
                {
                    LOG.Write(ex);
                    return;
                }
            }
        }

        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="byte_buffer"></param>
        /// <param name="length"></param>
        /// <param name="int_temp"></param>
        /// <returns></returns>
        private Result<List<Cfg_Field>> DeCode(byte[] byte_buffer, int length)
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            result.Data = new List<Cfg_Field>();
            byte[] buffer = new byte[8];
            byte[] dst = new byte[1000];

            try
            {
                int num;
                string str;
                int num12;
                int num41;
                if ((this.int_Count > 0) && ((this.int_Count + length) < 0x1f40))
                {
                    Buffer.BlockCopy(byte_buffer, 0, this.byte_DstCopy, this.int_Count, length);
                    this.int_Count += length;
                }
                else
                {
                    if (this.int_Count != 0)
                    {
                        goto Label_12C9;
                    }
                    Buffer.BlockCopy(byte_buffer, 0, this.byte_DstCopy, 0, length);
                    this.int_Count = length;
                }
                int num15 = 0;
                goto Label_1299;

            Label_007F:
                num = 0;
                while (num < (this.int_Count - 3))
                {
                    if ((this.byte_DstCopy[num] == 3) && (this.byte_DstCopy[num + 1] == 0) && (this.byte_DstCopy[num + 2] == 0) && (this.byte_DstCopy[num + 3] > 0))
                    {
                        goto Label_00CB;
                    }
                    num++;
                }
                goto Label_00CE;
            Label_00CB:
                num41 = num;
            Label_00CE:
                if (num41 == 0x270f)
                {
                    goto Label_12B6;
                }
                Buffer.BlockCopy(this.byte_DstCopy, num41, this.byte_DstCopy, 0, this.int_Count - num41);
                this.int_Count -= num41;
                if (this.byte_DstCopy[3] > this.int_Count)
                {
                    return result;
                }
                Buffer.BlockCopy(this.byte_DstCopy, 0, dst, 0, this.byte_DstCopy[3]);
                this.int_Count -= this.byte_DstCopy[3];
                Buffer.BlockCopy(this.byte_DstCopy, this.byte_DstCopy[3], this.byte_DstCopy, 0, this.int_Count);
                num15++;
                if ((dst[3] == 0x47) && ((dst[12] == 7) || ((dst[0x18] == this.byte_Feed2) && (this.byte_Feed2 > 0))))
                {
                    if (dst[12] == 7)
                    {
                        this.byte_Feed2 = dst[0x18];
                    }
                    if (dst[0x26] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x27 + j];
                        }
                        double num11 = BitConverter.ToDouble(buffer, 0);
                        string temp = Math.Abs(num11).ToString("f3");//进给实际值
                    }
                    if (dst[50] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x33 + j];
                        }
                        double num26 = BitConverter.ToDouble(buffer, 0);
                        string temp1 = Math.Abs(num26).ToString("f3");//进给设定
                    }
                    if (dst[0x3e] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x3f + j];
                        }
                        double num30 = BitConverter.ToDouble(buffer, 0);
                        //if (this.byte_16 == 0)
                        //{
                        string temp2 = Math.Abs(num30).ToString("f3");//进给倍率
                        //textBox16.Text = temp2;
                        //}
                    }
                    goto Label_128E;
                }
                if ((dst[3] == 0x2f) && ((dst[12] == 8) || ((dst[0x18] == this.byte_tool) && (this.byte_tool > 0))))
                {
                    if (dst[12] == 8)
                    {
                        this.byte_tool = dst[0x18];
                    }
                    buffer[0] = dst[0x27];
                    buffer[1] = dst[40];
                    short num46 = BitConverter.ToInt16(buffer, 0);
                    result.Data.Add(new Cfg_Field() { name= "刀具号", value= num46.ToString() });
                    buffer[0] = dst[0x2d];
                    buffer[1] = dst[0x2e];
                    result.Data.Add(new Cfg_Field() { name = "刀补号", value = BitConverter.ToInt16(buffer, 0).ToString() });
                    goto Label_128E;
                }
                if ((dst[3] == 0x47) && ((dst[12] == 9) || ((dst[0x18] == this.byte_Feed) && (this.byte_Feed > 0))))
                {
                    if (dst[12] == 9)
                    {
                        this.byte_Feed = dst[0x18];
                    }
                    if (dst[0x26] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x27 + j];
                        }
                        double num6 = BitConverter.ToDouble(buffer, 0);
                        if (this.byte_State > 0)
                        {
                            result.Data.Add(new Cfg_Field() { name = "进给实际值", value = Math.Abs(num6).ToString("f3") });
                        }
                    }
                    if (dst[50] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x33 + j];
                        }
                        double num29 = BitConverter.ToDouble(buffer, 0);
                        if (this.byte_State > 0)
                        {
                            result.Data.Add(new Cfg_Field() { name = "进给设定", value = Math.Abs(num29).ToString("f3") });
                        }
                    }
                    if (dst[0x3e] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x3f + j];
                        }
                        double num43 = BitConverter.ToDouble(buffer, 0);
                        if (this.byte_State > 0)
                        {
                            result.Data.Add(new Cfg_Field() { name = "进给倍率", value = Math.Abs(num43).ToString("f3") });
                        }
                    }
                    goto Label_128E;
                }
                if ((dst[3] == 0x47) && ((dst[12] == 10) || ((dst[0x18] == this.byte_Spindle) && (this.byte_Spindle > 0))))
                {
                    if (dst[12] == 10)
                    {
                        this.byte_Spindle = dst[0x18];
                    }
                    if (dst[0x26] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x27 + j];
                        }
                        double num42 = BitConverter.ToDouble(buffer, 0);
                        result.Data.Add(new Cfg_Field() { name = "主轴实际值", value = Math.Abs(num42).ToString("f3") });
                    }
                    if (dst[50] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x33 + j];
                        }
                        double num45 = BitConverter.ToDouble(buffer, 0);
                        result.Data.Add(new Cfg_Field() { name = "主轴设定值", value = Math.Abs(num45).ToString("f3") });
                    }
                    if (dst[0x3e] == 8)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            buffer[j] = dst[0x3f + j];
                        }
                        double num40 = BitConverter.ToDouble(buffer, 0);
                        result.Data.Add(new Cfg_Field() { name = "主轴倍率", value = Math.Abs(num40).ToString("f3") });
                    }
                    goto Label_128E;
                }
                if ((dst[3] == 0x2f) && ((dst[12] == 11) || ((dst[0x18] == this.byte_Op) && (this.byte_Op > 0))))
                {
                    if (dst[12] == 11)
                    {
                        this.byte_Op = dst[0x18];
                    }
                    if (dst[0x26] == 2)
                    {
                        string mode = string.Empty;
                        if (dst[0x2d] == 0)
                        {
                            if (dst[0x27] == 0)
                            {
                                mode = "Manual";//运行方式
                            }
                            if (dst[0x27] == 1)
                            {
                                mode = "Manual";
                            }
                            if (dst[0x27] == 2)
                            {
                                mode = "Automatic";
                            }
                            this.byte_State = dst[0x27];
                        }
                        else if (dst[0x2d] == 3)
                        {
                            mode = "Manual";
                            this.byte_State = 3;
                        }

                        result.Data.Add(new Cfg_Field() { name = "方式", value = mode });
                    }
                    goto Label_128E;
                }
                if ((dst[3] == 0x2f) && ((dst[12] == 12) || ((dst[0x18] == this.byte_Status) && (this.byte_Status > 0))))
                {
                    if (dst[12] == 12)
                    {
                        this.byte_Status = dst[0x18];
                    }
                    if (dst[0x26] == 2)
                    {
                        string state = string.Empty;
                        if ((dst[0x27] == 0) && (dst[0x2d] == 5))
                        {
                            state = "Standby"; //运行状态
                        }

                        if (((dst[0x27] != 2) || (dst[0x2d] != 2)) ? (dst[0x2d] == 1) : true)
                        {
                            state = "Running";
                        }
                        if ((dst[0x27] == 1) && (dst[0x2d] == 3))
                        {
                            state = "Running";
                        }
                        if ((dst[0x27] == 1) && (dst[0x2d] == 5))
                        {
                            state = "Standby";
                        }
                        if (((dst[0x27] == 2) && (dst[0x2d] == 5)))
                        {
                            state = "Emergency Stop";
                        }

                        result.Data.Add(new Cfg_Field() { name = "状态", value = state });
                    }
                    else if (dst[0x26] == 8 && dst[32] == 0x0E && dst[34] == 1)
                    {
                        //textBox39.Text = BitConverter.ToDouble(dst, 0x27).ToString();//R变量
                    }
                    goto Label_128E;
                }
                if ((dst[3] == 0x83) && ((dst[12] == 14) || ((dst[0x18] == this.byte_MachineXyz) && (this.byte_MachineXyz > 0))))
                {
                    if (dst[12] == 14)
                    {
                        this.byte_MachineXyz = dst[0x18];
                    }
                    int index = 0x23;
                    for (int j = 0; j < 12; j++)
                    {
                        if ((dst[index] == 0xff) && (dst[index + 1] == 9))
                        {
                            buffer[0] = dst[index + 4];
                            buffer[1] = dst[index + 5];
                            buffer[2] = dst[index + 6];
                            buffer[3] = dst[index + 7];
                            buffer[4] = dst[index + 8];
                            buffer[5] = dst[index + 9];
                            buffer[6] = dst[index + 10];
                            buffer[7] = dst[index + 11];
                            switch (j)
                            {
                                case 0:
                                    result.Data.Add(new Cfg_Field() { name = "机床坐标X", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    goto Label_0912;

                                case 1:
                                    result.Data.Add(new Cfg_Field() { name = "机床坐标Y", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    goto Label_0912;

                                case 2:
                                    result.Data.Add(new Cfg_Field() { name = "机床坐标Z", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;
                            }
                            if (j == 2)
                            {
                                //textBox21.Text = BitConverter.ToDouble(buffer, 0).ToString("f3");
                            }
                            else if (j != 3)
                            {
                                if (j == 4)
                                {
                                    result.Data.Add(new Cfg_Field() { name = "剩余坐标X", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                }
                                else if (j == 5)
                                {
                                    result.Data.Add(new Cfg_Field() { name = "剩余坐标Y", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                }
                                else if (j == 6)
                                {
                                    result.Data.Add(new Cfg_Field() { name = "剩余坐标Z", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                }
                                else if (j == 7)
                                {
                                }
                            }
                        }
                    Label_0912:
                        index += 12;
                    }
                    goto Label_128E;
                }
                if ((dst[3] != 0xc3) || ((dst[12] != 0x10) && ((dst[0x18] != this.byte_Alarm) || (this.byte_Alarm <= 0))))
                {
                    goto Label_0B30;
                }
                if (dst[12] == 0x10)
                {
                    this.byte_Alarm = dst[0x18];
                }
                if ((dst[0x23] == 0xff) && (dst[0x24] == 9))
                {
                    buffer[0] = dst[0x27];
                    buffer[1] = dst[40];
                    if ((dst[52] != 0) && (dst[53] != 0))
                    {
                        result.Data.Add(new Cfg_Field() { name = "报警时间", value = dst[52].ToString("X") + "/" + dst[53].ToString("X") + " " + dst[54].ToString("X") + ":" + dst[55].ToString("X") });
                    }
                    string str2 = BitConverter.ToInt16(buffer, 0).ToString();
                    bool flag3 = false;
                    bool flag = false;
                    if (dst[0xbb] == 1)
                    {
                        flag = true;
                    }
                    if (flag)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (str2 == this.string_40[j])
                            {
                                flag3 = true;
                            }
                        }
                        if (!flag3)
                        {
                            for (int k = 0; k < 4; k++)
                            {
                                if ((this.string_40[k] == "0") || (this.string_40[k] == ""))
                                {
                                    this.string_40[k] = str2;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (str2 == this.string_40[j])
                            {
                                this.string_40[j] = "0";
                            }
                        }
                    }
                    //textBox15.Text = "0";
                    num12 = 0;
                    while (num12 < 4)
                    {
                        if ((this.string_40[num12] != "0") && (this.string_40[num12] != ""))
                        {
                            goto Label_0AC4;
                        }
                        num12++;
                    }
                }
                goto Label_0AD3;
            Label_0AC4:
                result.Data.Add(new Cfg_Field() { name = "报警号", value = this.string_40[num12] });
            Label_0AD3:
                str = "";
                for (int i = 0; i < dst[3]; i++)
                {
                    str = str + dst[i].ToString("X") + " ";
                }
                //this.method_20("报警号：" + this.string_23 + "    " + str);
                goto Label_128E;
            Label_0B30:
                if ((dst[3] == 0xc7) && ((dst[12] == 0x12) || ((dst[0x18] == this.byte_ProName) && (this.byte_ProName > 0))))
                {
                    if (dst[12] == 0x12)
                    {
                        this.byte_ProName = dst[0x18];
                    }
                    if ((dst[0x23] != 0xff) || (dst[0x24] != 9))
                    {
                        goto Label_128E;
                    }
                    int index = 0x27;
                    string0 = "";
                    while (dst[index] != 0)
                    {
                        //Label_0BA3:
                        //if (dst[index + 1] == 0)
                        //{
                        //    goto Label_128E;
                        //}
                        string0 = string0 + Encoding.ASCII.GetString(dst, index, 1);//程序名
                        index++;
                    }

                    result.Data.Add(new Cfg_Field() { name = "程序名", value = string0 });
                    goto Label_128E;

                    //goto Label_0BA3;

                }
                if ((dst[3] == 0x53) && ((dst[12] == 20) || ((dst[0x18] == this.byte_WorkXyz) && (this.byte_WorkXyz > 0))))
                {
                    if (dst[12] == 20)
                    {
                        this.byte_WorkXyz = dst[0x18];
                    }
                    int index = 0x23;
                    for (int j = 0; j < 4; j++)
                    {
                        if ((dst[index] == 0xff) && (dst[index + 1] == 9))
                        {
                            buffer[0] = dst[index + 4];
                            buffer[1] = dst[index + 5];
                            buffer[2] = dst[index + 6];
                            buffer[3] = dst[index + 7];
                            buffer[4] = dst[index + 8];
                            buffer[5] = dst[index + 9];
                            buffer[6] = dst[index + 10];
                            buffer[7] = dst[index + 11];
                            switch (j)
                            {
                                case 0://工作坐标X
                                    result.Data.Add(new Cfg_Field() { name = "工件坐标X", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;

                                case 1://工作坐标Y
                                    result.Data.Add(new Cfg_Field() { name = "工作坐标Y", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;

                                case 2://工作坐标Z
                                    result.Data.Add(new Cfg_Field() { name = "工作坐标Z", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;
                            }
                        }
                        index += 12;
                    }
                }
                else if ((dst[3] == 0x35) && (dst[12] == 0x20))
                {
                    int index = 0x15;
                    for (int j = 0; j < 4; j++)
                    {
                        if (((dst[index] == 0xff) && (dst[index + 1] == 7)) && (dst[index + 3] == 4))
                        {
                            buffer[3] = dst[index + 4];
                            buffer[2] = dst[index + 5];
                            buffer[1] = dst[index + 6];
                            buffer[0] = dst[index + 7];
                            switch (j)
                            {
                                case 0://母线电压
                                    result.Data.Add(new Cfg_Field() { name = "母线电压", value = BitConverter.ToSingle(buffer, 0).ToString("f3") });
                                    break;

                                case 1://母线电流
                                    result.Data.Add(new Cfg_Field() { name = "母线电流", value = BitConverter.ToSingle(buffer, 0).ToString("f3") });
                                    break;

                                case 2://电机功率
                                    result.Data.Add(new Cfg_Field() { name = "电机功率", value = BitConverter.ToSingle(buffer, 0).ToString("f3") });
                                    break;

                                case 3://电机温度
                                    result.Data.Add(new Cfg_Field() { name = "电机温度", value = BitConverter.ToSingle(buffer, 0).ToString("f3") });
                                    break;
                            }
                            index += 8;
                        }
                    }
                }
                else if ((dst[3] == 0x53) && ((dst[12] == 0x11) || ((dst[0x18] == this.byte_Work) && (this.byte_Work > 0))))
                {
                    if (dst[12] == 0x11)
                    {
                        this.byte_Work = dst[0x18];
                    }
                    int index = 0x23;
                    byte[] buffer3 = new byte[8];
                    for (int j = 0; j < 4; j++)
                    {
                        if ((dst[index] == 0xff) && (dst[index + 1] == 9))
                        {
                            buffer3[0] = dst[index + 4];
                            buffer3[1] = dst[index + 5];
                            buffer3[2] = dst[index + 6];
                            buffer3[3] = dst[index + 7];
                            buffer3[4] = dst[index + 8];
                            buffer3[5] = dst[index + 9];
                            buffer3[6] = dst[index + 10];
                            buffer3[7] = dst[index + 11];
                            switch (j)
                            {
                                case 0:
                                    result.Data.Add(new Cfg_Field() { name = "运行时间", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;

                                case 1:
                                    result.Data.Add(new Cfg_Field() { name = "剩余时间", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;

                                case 2:
                                    result.Data.Add(new Cfg_Field() { name = "实际工件", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;

                                case 3:
                                    result.Data.Add(new Cfg_Field() { name = "需求工件", value = BitConverter.ToDouble(buffer, 0).ToString("f3") });
                                    break;
                            }
                        }
                        index += 12;
                    }
                }
                else if ((dst[12] == 15) || ((dst[0x18] == byte_Sys) && (byte_Sys > 0)))
                {
                    if (dst[12] == 15)
                    {
                        byte_Sys = dst[0x18];
                    }
                    int num3 = 0;
                    int num2 = 0x22;
                    if ((dst[0x23] == 0xff) && (dst[num2 + 2] == 9))
                    {
                        num3 = dst[num2 + 4];
                        string34 = "";
                        for (int j = 0; j < length; j++)
                        {
                            if (dst[(j + num2) + 5] == 0)
                            {
                                break;
                            }
                            string34 = string34 + Encoding.ASCII.GetString(dst, (j + num2) + 5, 1);//系统信息
                        }

                        result.Data.Add(new Cfg_Field() { name = "硬件序号", value = string34 });
                    }
                    num2 += 4;
                    num2 += num3;
                    if ((dst[num2 + 1] == 0xff) && (dst[num2 + 2] == 9))
                    {
                        num3 = dst[num2 + 4];
                        string35 = "";
                        for (int j = 0; j < length; j++)
                        {
                            if (dst[(j + num2) + 5] == 0)
                            {
                                break;
                            }
                            string35 = string35 + Encoding.ASCII.GetString(dst, (j + num2) + 5, 1);//系统信息
                        }

                        result.Data.Add(new Cfg_Field() { name = "版本信息", value = string35 });
                    }
                    num2 += 4;
                    num2 += num3;
                    if ((dst[num2 + 1] == 0xff) && (dst[num2 + 2] == 9))
                    {
                        num3 = dst[num2 + 4];
                        string36 = "";
                        for (int j = 0; j < length; j++)
                        {
                            if (dst[(j + num2) + 5] == 0)
                            {
                                break;
                            }
                            /* this.string_36 = string36 + Encoding.ASCII.GetString(dst, (j + num2) + 5, 1);//系统*/
                        }
                    }
                    num2 += 4;
                    num2 += num3;
                    if ((dst[num2 + 1] == 0xff) && (dst[num2 + 2] == 9))
                    {
                        num3 = dst[num2 + 4];
                        string37 = "";
                        for (int j = 0; j < length; j++)
                        {
                            if (dst[(j + num2) + 5] == 0)
                            {
                                break;
                            }
                            //this.string_37 = string37 + Encoding.ASCII.GetString(dst, (j + num2) + 5, 1);//系统信息
                        }
                    }
                    num2 += 4;
                    num2 += num3;
                    if ((dst[num2 + 1] == 0xff) && (dst[num2 + 2] == 9))
                    {
                        num3 = dst[num2 + 4];
                        string38 = "";
                        for (int j = 0; j < length; j++)
                        {
                            if ((dst[(j + num2) + 5] == 0x54) || (dst[(j + num2) + 5] == 0x74))
                            {
                                byte_SysTemp = 0;
                            }
                            else if ((dst[(j + num2) + 5] == 0x4d) || (dst[(j + num2) + 5] == 0x6d))
                            {
                                byte_SysTemp = 1;
                            }
                            if (dst[(j + num2) + 5] == 0)
                            {
                                break;
                            }
                            string38 = string38 + Encoding.ASCII.GetString(dst, (j + num2) + 5, 1);//系统信息
                        }

                        result.Data.Add(new Cfg_Field() { name = "系统型号", value = string38 });
                    }
                }
            Label_128E:
                if (this.int_Count <= 0)
                {
                    goto Label_12DB;
                }
            Label_1299:
                num41 = 0x270f;
                if (this.int_Count > 4)
                {
                    goto Label_007F;
                }
                return result;
            Label_12B6:
                this.int_Count = 0;
                return result;
            Label_12C9:
                this.int_Count = 0;
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        Label_12DB:
            result.Success = true;
            return result;
        }

        private byte[] Heart;
        /// <summary>
        /// 读系统信息发送命令
        /// </summary>
        private byte[] _systemInCMD = new byte[]{
            3, 0, 0, 0x53, 2, 240, 0x80, 50, 7, 0, 0, 0, 15, 0, 8, 0,
            0x3a, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x36, 0, 5, 0,
            0x12, 0x12, 8, 130, 1, 70, 110, 0, 1, 0x1a, 1, 0x12, 8, 130, 1, 70,
            120, 0, 1, 0x1a, 1, 0x12, 8, 130, 1, 70, 120, 0, 2, 0x1a, 1, 0x12,
            8, 130, 1, 70, 120, 0, 3, 0x1a, 1, 0x12, 8, 130, 1, 70, 120, 0,
            4, 0x1a, 1
        };

        /// <summary>
        /// 读工作坐标发送命令
        /// </summary>
        private byte[] _workXYZCMD = new byte[]{
            3, 0, 0, 0x49, 2, 240, 0x80, 50, 7, 0, 0, 0, 20, 0, 8, 0,
            0x30, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x2c, 0, 4, 0,
            0x11, 0x12, 8, 130, 0x41, 0, 0x19, 0, 1, 0x70, 1, 0x12, 8, 130, 0x41, 0,
            0x19, 0, 2, 0x70, 1, 0x12, 8, 130, 0x41, 0, 0x19, 0, 3, 0x70, 1, 0x12,
            8, 130, 0x41, 0, 0x19, 0, 4, 0x70, 1
        };

        /// <summary>
        /// 读报警信息发送命令
        /// </summary>
        private byte[] _alarmCMD = new byte[]{
            3, 0, 0, 0x2b, 2, 240, 0x80, 50, 7, 0, 0, 0, 0x10, 0, 8, 0,
            0x12, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 14, 0, 1, 13,
            0x11, 0x12, 8, 130, 1, 0, 0, 0, 1, 0x54, 1
        };

        /// <summary>
        /// 读状态命令
        /// </summary>
        private byte[] _statusCMD = new byte[]{
            3, 0, 0, 0x35, 2, 240, 0x80, 50, 7, 0, 0, 0, 12, 0, 8, 0,
            0x1c, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x18, 0, 2, 0,
            0x12, 0x12, 8, 130, 0x41, 0, 11, 0, 1, 0x7f, 1, 0x12, 8, 130, 0x41, 0,
            13, 0, 1, 0x7f, 1
        };

        /// <summary>
        /// 读
        /// </summary>
        private byte[] picIn = new byte[]{
            3, 0, 0, 0x49, 2, 240, 0x80, 50, 7, 0, 0, 0, 0x11, 0, 8, 0,
            0x30, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x2c, 0, 4, 0,
            0x11, 0x12, 8, 130, 0x41, 1, 0x29, 0, 1, 0x7f, 1, 0x12, 8, 130, 0x41, 1,
            0x2a, 0, 1, 0x7f, 1, 0x12, 8, 130, 0x41, 0, 0x79, 0, 1, 0x7f, 1, 0x12,
            8, 130, 0x41, 0, 0x77, 0, 1, 0x7f, 1
        };

        /// <summary>
        /// 读程序名发送
        /// </summary>
        private byte[] _programNameCMD = new byte[]{
            3, 0, 0, 0x2b, 2, 240, 0x80, 50, 7, 0, 0, 0, 0x12, 0, 8, 0,
            0x12, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 14, 0, 1, 0,
            0x12, 0x12, 8, 130, 0x41, 0, 12, 0, 1, 0x7a, 1
        };

        /// <summary>
        /// 读刀具号发送命令
        /// </summary>
        private byte[] _toolNumberCMD = new byte[]{
            3, 0, 0, 0x35, 2, 240, 0x80, 50, 7, 0, 0, 0, 8, 0, 8, 0,
            0x1c, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x18, 0, 2, 0,
            0x11, 0x12, 8, 130, 0x41, 0, 0x17, 0, 1, 0x7f, 1, 0x12, 8, 130, 0x41, 0,
            0x18, 0, 1, 0x7f, 1
        };

        /// <summary>
        /// 读主轴信息发送命令
        /// </summary>
        private byte[] _spInCMD = new byte[]{
            3, 0, 0, 0x3f, 2, 240, 0x80, 50, 7, 0, 0, 0, 10, 0, 8, 0,
            0x26, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x22, 0, 3, 0,
            0x11, 0x12, 8, 130, 0x41, 0, 2, 0, 1, 0x72, 1, 0x12, 8, 130, 1, 0,
            3, 0, 4, 0x72, 1, 0x12, 8, 130, 0x41, 0, 4, 0, 1, 0x72, 1
        };

        /// <summary>
        /// 读进给信息
        /// </summary>
        private byte[] _svInCMD = new byte[]{
            3, 0, 0, 0x3f, 2, 240, 0x80, 50, 7, 0, 0, 0, 7, 0, 8, 0,
            0x26, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x22, 0, 3, 0,
            0x11, 0x12, 8, 130, 0x41, 0, 3, 0, 1, 0x70, 1, 0x12, 8, 130, 0x41, 0,
            2, 0, 1, 0x70, 1, 0x12, 8, 130, 0x41, 0, 3, 0, 1, 0x7f, 1
        };

        /// <summary>
        /// 读工作剩余信息
        /// </summary>
        private byte[] _workLeftCMD = new byte[]{
            3, 0, 0, 0x71, 2, 240, 0x80, 50, 7, 0, 0, 0, 14, 0, 8, 0,
            0x58, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x54, 0, 8, 0,
            0x11, 0x12, 8, 130, 0x41, 0, 2, 0, 1, 0x74, 1, 0x12, 8, 130, 0x41, 0,
            2, 0, 2, 0x74, 1, 0x12, 8, 130, 0x41, 0, 2, 0, 3, 0x74, 1, 0x12,
            8, 130, 0x41, 0, 2, 0, 4, 0x74, 1, 0x12, 8, 130, 0x41, 0, 3, 0,
            1, 0x74, 1, 0x12, 8, 130, 0x41, 0, 3, 0, 2, 0x74, 1, 0x12, 8, 130,
            0x41, 0, 3, 0, 3, 0x74, 1, 0x12, 8, 130, 0x41, 0, 3, 0, 4, 0x74,
            1
        };

        /// <summary>
        /// 读功率信息
        /// </summary>
        private byte[] _readPoweCMD = new byte[]{
            3, 0, 0, 0x3b, 2, 240, 0x80, 50, 1, 0, 0, 0, 0x20, 0, 0x2a, 0,
            0, 4, 4, 0x12, 8, 130, 0xa1, 0, 0x1a, 0, 1, 130, 1, 0x12, 8, 130,
            0xa1, 0, 30, 0, 1, 130, 1, 0x12, 8, 130, 0xa1, 0, 0x21, 0, 1, 130,
            1, 0x12, 8, 130, 0xa1, 0, 0x25, 0, 1, 130, 1
        };

        /// <summary>
        /// 读操作模式信息
        /// </summary>
        private byte[] _oPCMD = new byte[]{
            3, 0, 0, 0x35, 2, 240, 0x80, 50, 7, 0, 0, 0, 11, 0, 8, 0,
            0x1c, 0, 1, 0x12, 4, 0x11, 2, 1, 0, 0xff, 9, 0, 0x18, 0, 2, 0,
            0x12, 0x12, 8, 130, 0x21, 0, 3, 0, 1, 0x7f, 1, 0x12, 8, 130, 0x41, 0,
            12, 0, 1, 0x7f, 1
        };

        private int int_Count;
        private byte[] byte_DstCopy = new byte[0x1f40];
        public string Recieve;

        private byte byte_tool = 0;
        private byte byte_Feed = 0;
        private byte byte_Spindle = 0;
        private byte byte_Op = 0;
        private byte byte_Status = 0;
        private byte byte_MachineXyz = 0;
        private byte byte_Sys = 0;
        private byte byte_Alarm = 0;
        private byte byte_Work = 0;
        private byte byte_ProName = 0;
        private byte byte_WorkXyz = 0;
        private byte byte_Feed2 = 0;
        private byte byte_SysTemp = 0;
        private byte byte_State = 0;

        //中间变量
        private string string0;
        private string string34;
        private string string36;//未使用，多余
        private string string37;//未使用，多余
        private string string38;
        private string string35;
        private string[] string_40 = new string[] { "0", "0", "0", "0" };
    }

    public class ByteClass
    {
        public byte[] byte_Temp;
        public ByteClass()
        {
            this.byte_Temp = new byte[150];
        }
    }
}
